﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SRF.Common;
using SRF.Admin;
using SRF.Admin.Models;

namespace SRF.Web.Areas.Admin.Controllers
{
    [SRF.Security.Authorize]
    [SRF.Common.Logging.Log]
    public class DataRuleController : Controller
    {
        static SRF.Common.Logging.ILog logger = SRF.Common.Logging.LogManager.GetLogger("DataRuleController");

        DataRuleService dataRuleService = ObjectHelper.GetObject<DataRuleService>();
        DataRuleItemService dataRuleItemService = ObjectHelper.GetObject<DataRuleItemService>();

        #region

        //
        // GET: /Admin/DataRule/  
        [SRF.UI.ViewTitle("数据规则列表")]
        public ActionResult List()
        {
            ViewBag.DataRuleList = new SelectList(dataRuleService.List(), "Id", "Name");

            return View();
        }

        /// <summary>
        /// 编辑数据规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns> 
        [SRF.UI.ViewTitle("数据规则列表")]
        public ActionResult Edit(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                ViewData["title"] = "新增数据规则";
                return View();
            }
            else
            {
                ViewData["title"] = "编辑数据规则";
                var dataRuleItem = dataRuleService.Get(id);

                return View(dataRuleItem);
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        [HttpPost]
        public ContentResult Edit(DataRule dataRule)
        {
            ViewBag.DataRuleList = dataRuleService.List();
            var id = dataRuleService.Save(dataRule);

            return Content(id);
        }

        public ActionResult Delete(string id)
        {
            dataRuleService.Delete(id);

            return Content("");
        }

        #endregion

        #region

        //
        // GET: /Admin/DataRule/  
        [SRF.UI.ViewTitle("数据规则列表")]
        public ActionResult ItemList()
        {
            SRF.UI.DataSource datasource;
            if (string.IsNullOrEmpty(Request["dataRuleId"]))
            {
                datasource = SRF.UI.DataSource.Empty;
            }
            else
            {
                datasource = new UI.DataSource("admin.dataRuleitem.list", "admin.dataRuleitem.count", m => { m.Like("name"); });
                datasource.PerPageSize = 10;
                ViewBag.DataRuleId = Request["dataRuleid"];
                ViewData["name"] = Request["name"];
            }

            return View(datasource);
        }
        /// <summary>
        /// 编辑数据规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>  
        public ActionResult ItemEdit(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                ViewData["title"] = "新增数据规则";
                return View(new DataRuleItem() { DataRuleId = Request["DataRuleId"] });
            }
            else
            {
                ViewData["title"] = "编辑数据规则";
                var dataRuleItem = dataRuleItemService.Get(id);

                return View(dataRuleItem);
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        [HttpPost]
        public ContentResult ItemEdit(DataRuleItem dataRuleItem)
        {
            var id = dataRuleItemService.Save(dataRuleItem);

            return Content(id);
        }
        /// <summary>
        /// 编辑数据规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>  
        public ActionResult ItemView(string id)
        {
            var dataRuleItem = dataRuleItemService.Get(id);

            return View(dataRuleItem);
        }

        public ActionResult DeleteItem(string id)
        {
            dataRuleItemService.Delete(id);

            return Content("");
        }

        #endregion

        protected override void OnException(ExceptionContext filterContext)
        {
            logger.Error(filterContext.RouteData.Values["action"], filterContext.Exception);

            base.OnException(filterContext);
        }
    }
}